// noinspection JSAnnotator

import {defineStore} from 'pinia'
import {searchSelfRouter} from "@/api/user/index.js";

export const useMenuStore = defineStore('menu', {
    state: () => ({
        menuList: [],
        routerList: [],
        tabList: [{title: '首页', path: '/index'}],
        activeTab: '/index'
    }),
    getters: {
        Array: (state) => state.menuList,
        Array: (state) => state.routerList,
        Array: (state) => state.tabList,
        String: (state) => state.activeTab,
    },
    actions: {
        setMenuList(data) {
            console.log("menuList: ", data)
            this.menuList = data;
        },
        setRouterList(data) {
            data.forEach(item => {
                let routerInfo = {
                    name: item.menuName,
                    path: item.path,
                    meta: {title: item.menuName},
                    component: () => import(`../views/${item.path}.vue`)
                }
                this.routerList.push(routerInfo)
                let childrenList = item.children;
                childrenList.forEach(children => {
                    let routerInfo = {
                        name: children.menuName,
                        path: children.path,
                        meta: {title: children.menu},
                        component: () => import(/* @vite-ignore */`../views/${children.path}.vue`)
                    }
                    this.routerList.push(routerInfo)
                })
            })
        },
        generateRouter() {
            return new Promise((resolve, reject) => {
                searchSelfRouter().then(res => {
                    if (res.data.code == 200) {
                        this.setRouterList(res.data.data)
                        resolve()
                    }
                    reject()
                })
            })
        },
        setTabList(data) {
            this.tabList.push(data)
        },
        delTabList(name) {
            this.tabList = this.tabList.filter(item => {
                if (item == name) {
                    return false
                } else {
                    return false
                }

            })
        },
        setActiveTab(name) {
            this.activeTab = name
        },
    },

    // persist: {
    //     enabled: true,
    //     storage: localStorage,
    //     key: 'useMenu',
    //     path: ['menuList', 'routerList']
    // }
})